package cn.itcast.es.config;

import cn.itcast.es.utils.GoodsUtil;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.stream.Stream;

/**
 * @author 虎哥
 */
@Configuration
public class ElasticConfig {
    @Bean
    public GoodsUtil goodsUtil(ElasticProperties prop){
        String hosts = prop.getHosts();
        String[] split = hosts.split(",");
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(Stream.of(split).map(HttpHost::create).toArray(HttpHost[]::new)));
        return new GoodsUtil(client);
    }
}
